package com.zf.shuashua.ui.custom;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;

import androidx.appcompat.widget.AppCompatTextView;

import com.lxj.xpopup.util.XPopupUtils;
import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.utils.Util;

import anet.channel.util.Utils;

public class CountDownView extends AppCompatTextView {
    private Paint mPaint;
    private int mAngel = 0;
    private ValueAnimator mValueAnimator;
    private RectF rectF;

    public CountDownView(Context context) {
        super(context);
        init();
    }

    public CountDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        rectF = new RectF();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.parseColor("#F82F2F"));
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(XPopupUtils.dp2px(ShuaApplication.getContext(), 2));
        mPaint.setStyle(Paint.Style.STROKE);
    }

    private long countDownMilli;

    public void start(long countDownMilli) {

        this.countDownMilli = countDownMilli;
        if (mValueAnimator != null) {
            mValueAnimator.cancel();
        }
        mValueAnimator = ValueAnimator.ofFloat(0, 100);
        mValueAnimator.setDuration(countDownMilli);
        mValueAnimator.setInterpolator(new LinearInterpolator());
        mValueAnimator.setRepeatCount(0);
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float i = Float.valueOf(String.valueOf(animation.getAnimatedValue()));
                mAngel = (int) (360 * (i / 100f));
                invalidate();
            }
        });
        mValueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                setAnimationRun(true);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                setAnimationRun(false);
                if (mCountDownViewListener != null) {
                    mCountDownViewListener.onFinish();
                }
                if (countDownMilli > 0) {
                    start(countDownMilli);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                setAnimationRun(false);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        mValueAnimator.start();
    }

    private boolean isAnimationRun;

    public boolean isAnimationRun() {
        return isAnimationRun;
    }

    public void setAnimationRun(boolean animationRun) {
        isAnimationRun = animationRun;
    }

    public void pause() {
        if (mValueAnimator != null) {
            mValueAnimator.pause();
        }
    }

    public void resume() {
        if (mValueAnimator != null) {
            mValueAnimator.resume();
        }
    }

    public void cancel() {
        if (mValueAnimator != null) {
            mValueAnimator.cancel();
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int mWidth = getMeasuredWidth();
        int mHeight = getMeasuredHeight();
        rectF.left = 5;
        rectF.top = 5;
        rectF.right = mWidth - 5;
        rectF.bottom = mHeight - 5;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(rectF, -90, mAngel, false, mPaint);
    }

    CountDownViewListener mCountDownViewListener;

    public void setCountDownViewListener(CountDownViewListener countDownViewListener) {
        mCountDownViewListener = countDownViewListener;
    }

    public interface CountDownViewListener {
        void onFinish();
    }

}
